﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.BLL;
using OgilvyOne.Model;

public partial class admin_manager_role_list : AdminBasePage
{
    public string userName;
    public string roleid = "";
    bool ischeck = false;
    string rolename;
    string user;
    string role="";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.PopedomValidate(layerid, MOD);
        user=new AdminBasePage().GetSessionUsersModel().Username;
        userName = Request.Params["name"];
        UsersModel model = new UsersBll().GetModel(userName);        
        DataSet ds = new RoleBll().GetList("");
        if (!IsPostBack)
        {
            header.Text = new ColumnBll().LayeridSysColumnNaiv(Layerid);
            Repeater2.DataSource = ds;
            Repeater2.DataBind();
            for (int j = 0; j < this.Repeater2.Items.Count; j++)
            {
                CheckBox check = (CheckBox)this.Repeater2.Items[j].FindControl("CheckBox1");

                RoleModel rolemodel = new RoleBll().GetModel(check.Text);
                rolename = rolemodel.Rolename;
                if (model.Userrole != null)
                {
                    string[] userrolelist = model.Userrole.Split(',');
                    foreach (string str in userrolelist)
                    {
                        if (rolemodel.Rid.ToString() == str)
                        {
                            check.Checked = true;
                        }
                    }
                }               
            }
        }

        if (IsPostBack)
        {
            //if (model.Userrole != null)
            //{
            //    roleid += model.Userrole;
            //}
            for (int j = 0; j < this.Repeater2.Items.Count; j++)
            {
                CheckBox check = (CheckBox)this.Repeater2.Items[j].FindControl("CheckBox1");

                RoleModel rolemodel = new RoleBll().GetModel(check.Text);
                if (check.Checked)
                {
                    ischeck = true;
                    //RoleModel rolemodel= new RoleBll().GetModel(check.Text);
                    roleid = roleid + rolemodel.Rid.ToString() + ",";
                    role = role + check.Text + " ";
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!ischeck)
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert('角色不能为空！');", true);
            return;
        }
        UsersModel usermodel = new UsersModel();
        usermodel = new UsersBll().GetModel(userName);
        roleid = roleid.Substring(0, roleid.LastIndexOf(","));
        if (roleid.Trim().Length < 1)
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert('角色不能为空！');", true);
            return;
        }
        usermodel.Userrole = roleid;
        if (new UsersBll().Update(usermodel))
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert('保存成功！');window.location=\"user_list.aspx?layerid="+Layerid+"\"", true);
            this.insertLog(user + "修改" + userName + "的角色" + rolename + "为 " + role);
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert('保存失败！');window.location=\"user_list.aspx\"", true);
        }
    }
}
